package com.aix.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.UUID;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        Demo();

//        Dog dog=new Dog();
//        dog.testwawa();

    }



    public static void Demo(){
        SkipList<String> list = new SkipList<String>();
        list.put(10, "sho");
        list.put(1, "sha");
        list.put(9, "na");
        list.put(2, "bing");
        list.put(8, "ling");
        list.put(7, "xiao");
        list.put(100, "你好，skiplist");
        list.put(5, "冰");
        list.put(6, "灵");
        System.out.println("列表元素：\n" + list);
        System.out.println("删除100：" + list.remove(100));
        System.out.println("列表元素：\n" + list);
        System.out.println("5对于的value：\n" + list.get(5).value);
        System.out.println("链表大小：" + list.size() + ",深度：" + list.getLevel());
        System.out.println(list.toString());
    }

    public static void Lian(){
        //        Node head=new Node(-1);

        //1个
        Node head = new Node(1);
//        System.out.println(head);
//        System.out.println(reverse(head));
//        //2个
//        head = new Node(1);
//        head.addToNext(new Node(2));
//        System.out.println(head);
//        System.out.println(reverse(head));
//        //3个
//        head = new Node(1);
//        head.addToNext(new Node(2))
//                .addToNext(new Node(3));
//        System.out.println(head);
//        System.out.println(reverse(head));
        //4个
        head = new Node(1);
        head.addToNext(new Node(2))
                .addToNext(new Node(3))
                .addToNext(new Node(4))
                .addToNext(new Node(5));
        System.out.println(head);
        System.out.println(reverseCopy2(head));
        //多个
//        head = new Node(1);
//        Node tempHead = head;
//        for (int i = 2; i <= 15; i++) {
//            head = head.addToNext(new Node(i));
//        }
//        System.out.println(tempHead);
//        System.out.println(reverse(tempHead));

    }

    public static Node jian(Node head){

            Node pre = head; //前驱节点
            Node cur = null; //当前节点
            if(head.hasNext()){ //节点数量超过一个的时候
                cur = head.getNext();
            }else{
                return head;
            }
            pre.setNext(null); //依旧先切前驱节点的与原链表的联系
            while(cur.hasNext()){
                Node after = cur.getNext(); //标记出来下一个操作的节点
                cur.setNext(pre); //当前节点指向前驱节点
                pre = cur; //现有当前节点改为前驱节点
                cur = after; //现有下一个节点 改为当前需要操作的节点
            }
            cur.setNext(pre); //到最末尾的时候直接反转，cur为新的头节点
            return cur;


    }

    public static Node reverseCopy2(Node head){
        Node cur = head; //前驱节点
        Node pre = null; //当前节点
        while(cur.hasNext()){
            Node after = cur.getNext(); //标记出来下一个操作的节点
            cur.setNext(pre); //当前节点指向前驱节点
            pre = cur; //现有当前节点改为前驱节点
            cur = after; //现有下一个节点 改为当前需要操作的节点
        }
        cur.setNext(pre); //到最末尾的时候直接
        return cur;
    }

    public static Node reverse1(Node head){
        //空链表 或者 已经到了尾节点 不判断尾节点会空指针。
        if(head == null || !head.hasNext()){
            return head;
        }else{
            //取出下一个节点
            Node nextNode = head.getNext();
            //先作为下一次调用的参数传到下一个节点，切记不能先处理再传
            Node retNode = reverse1(nextNode);
            nextNode.setNext(head); //下一节点连到当前节点
            head.setNext(null); //将当前操作节点与原有链表截断
            return retNode;
        }
    }

    public static Node reverse(Node head) {
        Node curNode = null;
        Node afterNode = null;
        if (head == null) {
            return null;
        }
        if (head.hasNext()) {
            curNode = head.getNext();
        } else {
            return head; //假如只有一个节点，则不反转
        }
        if (curNode.hasNext()) {
            afterNode = curNode.getNext();
        } else {
            curNode.setNext(head); //假如链表只有两个节点 直接反转引用
            head.setNext(null);
            return curNode;
        }
        //假如有3个及3个以上
        //先将第一个节点独立，如果不独立则这个节点将永远指向原链表中的第二个节点，则会成环。必须切断引用。
        head.setNext(null);
        Node newHead = head;//作为尾节点
        while (afterNode.hasNext()) {
            curNode.setNext(newHead);
            newHead = curNode;
            curNode = afterNode;
            afterNode = afterNode.getNext();
        }
        curNode.setNext(newHead);
        afterNode.setNext(curNode);
        return afterNode;
    }


}
